import { Controller, Get, Post, Body, Param, Put, Delete } from 'routing-controllers';
import { Container } from 'typedi';
import { {{pascalCase name}}Service } from '@services/{{dashCase name}}.service';
import { Create{{pascalCase name}}Dto, Update{{pascalCase name}}Dto } from '@/dtos';
import { Log, LogBizId, LogOperation } from '@decorators/logs.decorators';
import { OpenAPI } from 'routing-controllers-openapi';
import { RequiredPerm } from '@decorators/perm.decorator';

@Controller('/{{dashCase name}}s')
export class {{pascalCase name}}Controller {
  private {{camelCase name}} = Container.get({{pascalCase name}}Service);

  @Get('/page')
  @RequiredPerm('{{dashCase name}}:page')
  @OpenAPI({ summary: '获取 分页数据' })
  @Log({ bizType: '{{dashCase name}}', operation: LogOperation.QUERY, bizId: (req: { user: { id: string } }) => req?.user?.id ?? '' })
  async findAll() {
    return this.{{camelCase name}}.findAll();
  }

  @Get('/detail/:id')
  @RequiredPerm('{{dashCase name}}:detail')
  @OpenAPI({ summary: '获取详细数据' })
  @Log({ bizType: '{{dashCase name}}', operation: LogOperation.QUERY, bizId: (req: { user: { id: string } }) => req?.user?.id ?? '' })
  async findOne(@Param('id') id: number) {
    return this.{{camelCase name}}.findOne(id);
  }

  @Post('/create')
  @RequiredPerm('{{dashCase name}}:create')
  @OpenAPI({ summary: '创建' })
  @Log({ bizType: '{{dashCase name}}', operation: LogOperation.CREATE, bizId: (req: { user: { id: string } }) => req?.user?.id ?? '' })
  async create(@Body() dto: Create{{pascalCase name}}Dto) {
    return this.{{camelCase name}}.create(dto);
  }

  @Put('/update/:id')
  @RequiredPerm('{{dashCase name}}:update')
  @OpenAPI({ summary: '修改' })
  @Log({ bizType: '{{dashCase name}}', operation: LogOperation.UPDATE, bizId: (req: { user: { id: string } }) => req?.user?.id ?? '' })
  async update(@Param('id') id: number, @Body() dto: Update{{pascalCase name}}Dto) {
    return this.{{camelCase name}}.update(id, dto);
  }

  @Delete('/delete/:id')
  @RequiredPerm('{{dashCase name}}:delete')
  @OpenAPI({ summary: '删除' })
  @Log({ bizType: '{{dashCase name}}', operation: LogOperation.DELETE, bizId: (req: { user: { id: string } }) => req?.user?.id ?? '' })
  async remove(@Param('id') id: number) {
    return this.{{camelCase name}}.remove(id);
  }
}
